import Vue from "vue"
import VueRouter from "vue-router";

Vue.use(VueRouter);

import Home from "../pages/Home/index.vue";
import List from "../pages/List";
import ShoppingCart from "../pages/ShoppingCart";
import User from "../pages/User";
import UpdateDate from "../pages/User/UpdateDate";
import UpdatePwd from "../pages/User/UpdatePwd";
import UpdatePict from "../pages/User/UpdatePict";
import UpdateSite from "../pages/User/UpdateSite";
import Indent from "../pages/Indent";
import Details from "../pages/Details";
import Pay from "../pages/Pay";

// 重写 VueRouter 原型上的 push() 和 replace() 方法
let myPush = VueRouter.prototype.push;
let myReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (options, resolve = () => { }, reject = () => { }) {
  myPush.call(this, options, resolve, reject);
}
VueRouter.prototype.replace = function (options, resolve = () => { }, reject = () => { }) {
  myReplace.call(this, options, resolve, reject);
}


const router = new VueRouter({
  routes: [
    {
      name: "home",
      path: "/home",
      component: Home,
      meta: {
        title: "独白蛋糕",
      },
    },
    {
      path: "/",
      redirect: "/home",
    },
    {
      name: "list",
      path: "/list/:kind?",
      component: List,
      meta: {
        title: "商品列表",
      },
    },
    {
      name: "shoppingCart",
      path: "/shoppingCart",
      component: ShoppingCart,
      meta: {
        isAuth: true,
        title: "购物车",
      },
    },
    {
      // name: "user",
      path: "/user",
      component: User,
      meta: {
        isAuth: true,
        title: "我的信息"
      },
      children: [
        {
          name: "updateDate",
          path: "updateDate",
          component: UpdateDate,
          meta: {
            isAuth: true,
            title: "我的信息",
          },
        },
        {
          path: "",
          redirect: "updateDate",
        },
        {
          name: "updatePwd",
          path: "updatePwd",
          component: UpdatePwd,
          meta: {
            isAuth: true,
            title: "修改密码",
          },
        },
        {
          name: "updatePict",
          path: "updatePict",
          component: UpdatePict,
          meta: {
            isAuth: true,
            title: "我的头像",
          },
        },
        {
          name: "updateSite",
          path: "updateSite",
          component: UpdateSite,
          meta: {
            isAuth: true,
            title: "收货信息",
          },
        }
      ]
    },
    {
      name: "indent",
      path: "/indent",
      component: Indent,
      meta: {
        isAuth: true,
        title: "订单"
      },

    },
    {
      name: "details",
      path: "/details",
      component: Details,
      meta: {
        title: "详情"
      },
    },
    {
      name: "pay",
      path: "/pay",
      component: Pay,
      meta: {
        isAuth: true,
        title: "下单"
      },
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    if (localStorage.getItem("isLogin") !== "true") {
      return alert("你还没有登录，登录一下再访问吧");
    }
    next();
  } else {
    next();
  }
});

router.afterEach((to, from) => {
  document.title = to.meta.title || "独白蛋糕";
});

export default router;